<template>
	<view class="version">
		<u-navbar
			title="系统配置" 
			left-text="" 
			@leftClick="onClickBack"
			:placeholder="true"
			:fixed="true"
			:safeAreaInsetTop="true"
		></u-navbar>
		<view class="w-flex w-flex-center w-mt-20">
			<view class="w-center">
				<u-avatar :src="userInfo.avatar" shape="circle" size="60" style="margin: auto;"></u-avatar>
				
				<text class="w-flex w-mt-20 w-font-20">当前版本：{{version}}</text>
				<view class="w-mt-20 w-flex w-flex-center">
					<text @click="checkVersion()" class="versionBtn">检查新版本</text>
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
export default {
	data () {
		return {
			version: '1.0.0',
			userInfo: {}
		}
	},
	onLoad() {
		// #ifdef APP-PLUS
		plus.runtime.getProperty(plus.runtime.appid,(wgtinfo)=>{
			this.version = wgtinfo.version
		})
		// #endif
		this.userInfo = uni.getStorageSync("YNF_INFO")
	},
	methods:{
		onClickBack () {
			uni.navigateBack()
		},
		checkVersion () {
			// #ifdef APP-PLUS
			uni.request({
				url: 'https://ynf.xxhf.cn/index.php/api/common/get_app_version',
				method: 'GET',
				success: result => {
					if (result.data.code === 1) {
						plus.runtime.getProperty(plus.runtime.appid, function(inf) {
							let mobileType = ''
							if (plus.os.name === "Android") {
								
								mobileType = result.data.data.android_agent
							} else {
								mobileType = result.data.data.ios_agent
							}
							if (inf.version !== mobileType.version) {
								const dtask = plus.downloader.createDownload(mobileType.url, {force: true}, function ( d, status ) {
									if ( status === 200 ) { // 下载成功    
											var path = (d.filename);    
											plus.runtime.install(path,{},function(){
												 plus.nativeUI.closeWaiting();
												 plus.nativeUI.alert("应用资源更新完成！",function(){
														 plus.runtime.restart();
												 });
										 },function(e){
												 plus.nativeUI.closeWaiting();
												 plus.nativeUI.alert("安装wgt文件失败["+e.code+"]："+e.message);
										 });
									} else {//下载失败    
											alert( "Download failed: " + status );     
									}      
								});
								uni.showModal({  
									title:'版本更新',  
									content: mobileType.content,  
									confirmText:'下载',  
									success: (res) => {  
										if(res.confirm) {  
												dtask.start();   
												let prg = 0;  
												let showLoading = plus.nativeUI.showWaiting("正在下载");  
												dtask.addEventListener('statechanged', function(task, status) {
														// 给下载任务设置一个监听 并根据状态  做操作  
														switch (task.state) {
																case 1:  
																	showLoading.setTitle("正在下载");  
																	break;  
																case 3:  
																	prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100);  
																	showLoading.setTitle("  正在下载" + prg + "%  ");  
																	break;  
																case 4:  
																	 plus.nativeUI.closeWaiting();  
																		//下载完成  
																	break;  
														}  
												})  
										}  
									}  
								})
							} else {
								uni.showToast({
									title: '当前是最新版本',
									icon: 'none',
									duration: 2000,
									success: (res)=> {
										
									}
								})
							}
						});
					}
				},
			})
			// #endif
		}
	}
}
</script>

<style lang="scss" scoped>
.version {
	.versionBtn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 270rpx;
		height: 80rpx;
		background: #1678ff;
		color: #FFFFFF;
		cursor: pointer;
		border-radius: 12px;
	}

}
</style>
